package cn.itcast.web.task;


import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.system.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

public class MyTask {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Reference
    private ContractService contractService;
    @Autowired
    private UserService userService;
    /**
     * 定时执行的任务方法，比如：每5秒钟执行一次execute()方法
     */
    public void execute(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, 3);
        List<Contract> contractList = contractService.findByDeliveryPeriod(sdf.format(c.getTime()) + "%");
        for (Contract contract : contractList) {
            User user = userService.findById(contract.getCreateBy());
            Map<String, String> map = new HashMap<>();
            map.put("title", "交货到期提示");
            map.put("content", "合同号为"+contract.getContractNo()+"的货物3天后将到交货期，请注意！");
            map.put("email", user.getEmail());
            rabbitTemplate.convertAndSend("msg.email",map);
        }
    }
}
